package com.SouthernWall_404.age_tool.common.world.block;

import com.SouthernWall_404.age_tool.common.API.GeneralAPI;
import com.SouthernWall_404.age_tool.common.Network.ModChannels;
import com.SouthernWall_404.age_tool.common.world.Entity.MusicBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

public class MusicBlock extends HorizontalDirectionalBlock implements EntityBlock {

    public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;

    public MusicBlock(Properties p_49795_) {
        super(p_49795_);
        // 2. 注册默认状态（朝北）
        this.registerDefaultState(this.stateDefinition.any()
                .setValue(FACING, Direction.NORTH));
    }
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        // 获取玩家水平朝向（取反使玩家面对方向成为方块正面）
        Direction facing = context.getHorizontalDirection().getOpposite();

        // 可选：特殊放置逻辑（如贴在墙面）
        if (context.getClickedFace().getAxis().isVertical()) {
            facing = context.getHorizontalDirection().getOpposite();
        } else {
            facing = context.getClickedFace();
        }

        return this.defaultBlockState().setValue(FACING, facing);
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(FACING);
        super.createBlockStateDefinition(builder);
    }


    @Override
    public @Nullable BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
        return new MusicBlockEntity(blockPos,blockState);
    }

    @Override
    public InteractionResult use(BlockState pState, Level level, BlockPos blockPos, Player player, InteractionHand pHand, BlockHitResult pHit) {

        if (level.isClientSide) {
            return InteractionResult.SUCCESS;
        } else {
            BlockEntity blockEntity = level.getBlockEntity(blockPos);
            if (blockEntity instanceof MusicBlockEntity musicBlockEntity && player instanceof ServerPlayer serverPlayer) {
                NetworkHooks.openScreen(serverPlayer, musicBlockEntity,blockPos);
            }
//            player.sendSystemMessage(Component.translatable("1"));
            return InteractionResult.CONSUME;
        }
    }

    @Override
    public @Nullable <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> blockEntityType) {
        if(level.isClientSide)
        {
            return (level1,blockpos,state,blockEntity)->{
                if(blockEntity instanceof MusicBlockEntity workBenchEntity)
                {
                    if (level.getGameTime() % 10 == 0) {
                        // 低频更新代码...
                        MusicBlockEntity musicBlockEntity=(MusicBlockEntity) blockEntity;
                        musicBlockEntity.startPlay();

                    }
                }
            };
        }
        else//server
        {
            return (level1,blockpos,state,blockEntity)->{
                if(blockEntity instanceof MusicBlockEntity workBenchEntity)
                {}
            };
        }
    }




}


